﻿using System.Collections.Generic;
using System.Linq;
using CID.Domain.Operations;
using CID.Domain.Users;
using CID.Web.Models.Operations;
using CID.Web.Support;
using NailsFramework.IoC;
using NailsFramework.Persistence;

namespace CID.Web.Controllers
{
    [CIDAuthorize(UserRole.Architect, UserRole.Manager)]
    public class ServerRoleController : AjaxEnabledBaseCRUDController<IServerRole, ServerRole, ServerRoleViewModel>
    {
        #region Helper members

        [Inject]
        public IBag<Environment> Environments { private get; set; }
        private IEnumerable<Environment> EnabledEnvironments { get { return Environments.Where(x => x.Enabled); } }

        [Inject]
        public IBag<Server> Servers { private get; set; }
        private IEnumerable<Server> EnabledServers { get { return Servers.Where(x => x.Enabled); } }
        #endregion

        protected override ServerRole CreateModel(ServerRoleViewModel viewModel)
        {
            return new ServerRole(viewModel.Name, viewModel.Description);
        }

        protected override void PrepareViewModelForView(ServerRoleViewModel viewModel)
        {
            base.PrepareViewModelForView(viewModel);

            viewModel.SetAvailableServers(EnabledServers);
            viewModel.SetAvailableEnvironments(EnabledEnvironments);
        }
    }
}